//index.js
//获取应用实例
const app = getApp();
import HttpRequst from '../../../utils/httpRequst.js'
import API from '../../../utils/config.js'
import WxValidate from '../../../plugin/WxValidate'
import SendSMS from '../../../utils/sendSMS.js'
import SendFormId from '../../../utils/sendFormId.js'

Page({
  data: {
    telphone: "",
    isyzmRuning:false,
    isSubmiting:false,
    validateTipMsg: "",
    yzmRemainTime:60,
    isYzmInputing:"",//是否验证码输入期间
    validateTipIsShow: false
  },

  onLoad: function() {
    this._initValidate();
   
  },

  // formForIdSubmit:function(e){
  //   app.getFormIds(e.detail.formId);
  // },
  
  //表单提交
  formSubmit: function(e) {
    const params = e.detail.value;
    var that=this;

    
   

    //验证表单
    if (!this.WxValidate.checkForm(e)) {
      const error = this.WxValidate.errorList[0];
      this.setData({
        validateTipMsg: error.msg,
        validateTipIsShow: true
      })
      this._validateTipHide();
    } else {
      //提交后端
      //HttpRequst(loading,url,发送header类型, sessionId, params,method, ask, function (res){})
      this.setData({
        isSubmiting: true
      });
      wx.showLoading();
     
      wx.login({
        success: function (res) {
          if (res.code) {
            params['wx_code']=res.code;
            wx.getUserInfo({
              withCredentials:true,
              success: function (res_user) {
                params['encryptedData']=res_user.encryptedData;
                params['iv']=res_user.iv;
                HttpRequst(false, API.login, 1, "", params, "POST", false, function (res) {
                  wx.hideLoading();
                  that.setData({
                    isSubmiting: false
                  })
                  app.globalData.okey=res.data.okey;
                  if (!!res.status) {
                    //通过了可以登录 本地存储用户登录的信息
                    app.globalData.userInfo = res.data;
                    wx.setStorage({
                      key: "userinfo",
                      data: res.data
                    });
                    app.getFormIds(e.detail.formId);
                    
                    SendFormId(app, "POST", 2, "");
                    wx.reLaunch({
                      url: '/pages/index/index/index'
                    });
                  } else {
                    //没有通过就显示服务端返回的错误提示信息
                    that.setData({
                      validateTipMsg: res.msg,
                      validateTipIsShow: true
                    });
                    that._validateTipHide();
                  }
                });
              }
            })
          } else {
            
          }
        },
        complete:function(){
         
          
        }
      })
    }
  },
  //实时获取手机号码 
  getTelphoneVal: function(e) {
    this.setData({
      telphone: e.detail.value
    });
    
  },
  //点击获取验证码
  //进行手机验证
  getCode: function() {
    if(this.data.isyzmRuning) return;
    var that=this;
    if (this.data.telphone && /^1[23456789]\d{9}$/.test(this.data.telphone)) {
      //验证通过发送手机号码到服务端
      // sendSMS(params, method, loading, sessionChoose, callBack)
      this.setData({
        isyzmRuning:true
      })
      SendSMS({mobile:this.data.telphone},"POST",false,2,function(res){
       
       
        if (!res.status){
          wx.showToast({
            title: '获取失败',
            icon: 'none',
            duration: 2000
          })
        }else{
          //服务端发送短信成功
          that.setData({
            isYzmInputing: true
          })
          that._countdownYzm();
        }

        that.setData({
          isyzmRuning: false
        })
        
      });
    } else {
      this.setData({
        validateTipMsg: "请输入正确的手机号码！",
        validateTipIsShow: true
      });
      this._validateTipHide();
    }
  },
  _countdownYzm: function () {
    var that = this;
    var Timer = setInterval(function () {
      if (that.data.yzmRemainTime <= 1) {
        clearInterval(Timer);
        that.setData({
          yzmRemainTime: 60,
          isYzmInputing: false
        });
      };
      that.setData({
        yzmRemainTime: that.data.yzmRemainTime - 1
      });
    }, 1000)
  },
  //2秒后隐藏错误信息提示
  _validateTipHide: function() {
    var that = this;
    setTimeout(function() {
      that.setData({
        validateTipIsShow: false
      })
    }, 2000);
  },
  _initValidate: function() {
    const rules = {
      mobile: {
        required: true,
        tel: true
      },
      code: {
        required: true
      }
    };
    const messages = {
      mobile: {
        required: "请输入您的手机号码",
        tel: "请输入正确的手机号码"
      },
      code: {
        required: "请输入验证码"
      }
    };
    this.WxValidate = new WxValidate(rules, messages);
  }

})